#include <stdlib.h>
#include <memory>

#include "HttpServer.hpp"


using namespace server;

static void Usage(string proc)
{
    cout << "\nUsage:\n\t" << proc << " local_port\n\n";
}
string suffixToDesc(const string suffix)
{
    string ct = "Content-Type: ";
    if(suffix == ".html")
    {
        ct += "text/html";
    }
    else if(suffix == ".jpg")
    {
        ct += "application/x-jpg;image/jpeg";
    }

    ct += "\r\n";
    return ct;
}
// 1.服务器和网页分离
// 2.url -> / : web根目录
// 3.要正确的给客户端返回资源类型(在响应报头里面要添加这样的字段), 我们要首先自己知道请求的资源类型是什么！我们怎么知道这个类型呢？
//   所有的资源都有后缀！在path里面我们可以找到以.作为分隔符的后缀
bool Get(const HttpRequest &req, HttpResponse &resp)
{
    // 1.for test 看看http请求是什么样子的---
    cout << "---------------------http start--------------------------" << endl;
    cout << req._inbuffer << endl;
    cout << "method: " << req._method << endl;
    cout << "url: " << req._url << endl;
    cout << "httpversion: " << req._httpversion << endl;
    cout << "path: " << req._path << endl;
    cout << "suffix: " << req._suffix << endl;
    cout << "size: " << req._size << "字节" << endl;
    cout << "---------------------http end  --------------------------" << endl;

    string respline = "HTTP/1.1 200 OK\r\n";
    // string respline = "HTTP/1.1 307 Temporary Redirect\r\n";//状态行

    // string respheader = "Content-Type: text/html\r\n";
    string respheader = suffixToDesc(req._suffix);

    respheader += "Set-Cookie: 1234567abcdefg; Max-Age=86440\r\n";
    // 往后，每次http请求，都会自动携带曾经设置的所有的cookie，帮服务器进行鉴权。--- 进而进行http会话保持

    // respheader += "Location: https://blog.csdn.net/erridjsis?spm=1011.2415.3001.5343\r\n";//临时重定向到csdn首页网站上

    string respblank = "\r\n";
    // string respbody = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>Test the label</title>\
    <h1>hello world</h1></head><body><p>春风若有怜花意，可否许我再少年</p></body></html>"; // 响应报文，我们先硬编码写一个标签性的网页
    string respbody;//这个respbody我们不想硬编码写到服务器里面，而是想直接从文件里面拿到，所以我们把请求路径path放到respbody里面
    respbody.resize(req._size + 1);
    if(!Util::readFile(req._path, (char*)respbody.c_str(), req._size))
    {
        // cout << "xxxxxxxxxxxxxxxx"<<endl;
        Util::readFile(html_404, (char*)respbody.c_str(), req._size);//如果失败则将404的内容放到respbody响应正文里面。
    }
    if(req._size > 0)
    {
        respheader += "Content-Length: ";
        respheader += to_string(respbody.size());
        respheader += "\r\n";
    }

    resp._outbuffer += respline;
    resp._outbuffer += respheader;
    resp._outbuffer += respblank;
    
    cout << "---------------------response start--------------------------" << endl;
    cout << resp._outbuffer << endl;
    cout << "---------------------response end  --------------------------" << endl;

    resp._outbuffer += respbody;

    return true;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, port)); // 缺省端口号为8080
    httpsvr->initServer();
    httpsvr->start();

    return 0;
}
